<?php
/**
 * Created by PhpStorm.
 * User: gyccvvh
 * Date: 2018-06-30
 * Time: 14:20
 */

namespace app\merchant\service;


use app\common\service\Service;
use think\Db;

class Archives extends Service
{
    protected $archives_map = [
        "area" => "Area",
        "intent" => "Intent",
        "scene" => "Scene",
        "season" => "Season",
        "company" => "Company",
        "express"   => "Express"
    ];

    public function getList($scene,$mer_id,$_form) {
        $query = Db::name($this->archives_map[$scene])
            ->where("mer_id",$mer_id);
        if(isset($_form['name']) && !empty($_form['name'])){
            $query->where("name","like","%{$_form['name']}%");
        }
        $records = $query->paginate(15,false,[
            "query" => $_form
        ]);
        if($records) {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
                ->setData("records",$records);
        }else {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Error)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Error);
        }
    }

    public function getOne($scene, $id) {
        $one = Db::name($this->archives_map[$scene])->where("id",$id)->find();
        if($one) {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
                ->setData("one",$one);
        }else {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Error)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Error);
        }
    }

    public function add($scene, $_form) {
        $res = Db::name($this->archives_map[$scene])->insert($_form);
        if($res){
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Add_Success);
        }else {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Error)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Add_Error);
        }
    }

    public function getAreaList($_mer_id,$name) {
        $query = Db::name("Area");
        if(isset($name) && !empty($name)) {
            $query->where("name","like","%{$name}%");
        }
        $records = $query->where("mer_id",$_mer_id)->select();
        $tree = new \thans\Common\tree($records);
        $list = $tree->formatArrayForTree('id', 'pid', 'name');
        return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
            ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Success)
            ->setData("records",$list);
    }

    public function getAreas($mer_id,$selected) {
        $list = Db::name("Area")->where("mer_id",$mer_id)->select();
        $tree = new \thans\Common\tree($list);
        $tree->formatArrayForTree('id', 'pid', 'name');
        $data = $tree->get_tree(0, "<option value='\$id' \$selected>\$spacer \$name </option>", $selected);
        return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
            ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Success)
            ->setData("records",$data);
    }


    public function edit($scene, $_form, $id) {
        $res = Db::name($this->archives_map[$scene])->where("id",$id)->update($_form);
        if($res) {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Edit_Success);
        }else {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Error)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Edit_Error);
        }
    }

    public function del($scene,$id){
        $res = Db::name($this->archives_map[$scene])->where("id",$id)->delete();
        if($res){
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Success)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Delete_Success);
        }else {
            return $this->ret->setCode(\app\merchant\define\Merchant::$Code_Mer_Error)
                ->setMsg(\app\merchant\define\Merchant::$Msg_Mer_Delete_Error);
        }
    }

}